package gui;

import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JScrollPane;

public class SuspendResumePanel extends JPanel implements ChatIF{
	
	
		private int eflag=0;
		
		private AcademicFrame frame;
		private SuspendResumePanel suspendResumePanel;
		private SupervisorPanel supervisorPanel;
		private JComboBox<String> rcomboBox;
		private JComboBox <String> comboBox;
		private ViewRequestPanel viewRequest;
		private JTextArea reasonArea;
		public SuspendResumePanel(AcademicFrame frame,SupervisorPanel supervisorPanel)
		{
			super();
			this.frame=frame;
			this.supervisorPanel=supervisorPanel;
			this.suspendResumePanel=this;
			frame.setSize(500, 400);
			initialize();
			
		}
		
		
	private void initialize() {
		
		
		setLayout(null);
		
		JLabel lblSuspendResume = new JLabel("Suspend & Resume");
		lblSuspendResume.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblSuspendResume.setForeground(Color.BLUE);
		lblSuspendResume.setBounds(121, 27, 217, 22);
		add(lblSuspendResume);
		
		JLabel lblInOrderTo = new JLabel("In order to suspend:");
		lblInOrderTo.setBounds(30, 74, 127, 14);
		add(lblInOrderTo);
		
		rcomboBox = new JComboBox();
		rcomboBox.setBounds(30, 109, 127, 20);
		add(rcomboBox);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(213, 113, 192, 48);
		add(scrollPane);
		
		reasonArea = new JTextArea();
		scrollPane.setViewportView(reasonArea);
		
		JButton btnDetails = new JButton("Details");
		btnDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))// filter invalid choice
				{
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				}
				else{
				viewRequest = new ViewRequestPanel(rcomboBox.getSelectedItem().toString(),frame,suspendResumePanel,false,"");
				frame.setSize(550, 780);
				frame.setContentPane(viewRequest);
				}
			}
		});
		btnDetails.setBounds(54, 140, 89, 23);
		add(btnDetails);
		
		JButton btnSuspend = new JButton("Suspend");
		btnSuspend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))
				{
				(new Dialog("Please select a Request")).setVisible(true);
				}
				else
					if(reasonArea.getText().equals(""))
					{
						(new Dialog("Must enter a Reason")).setVisible(true);
					}
					else
							{
						ArrayList<Object> suspend =new ArrayList<Object>();
						suspend.add("suspend");
						suspend.add(rcomboBox.getSelectedItem());
						suspend.add(reasonArea.getText());
						AcademicFrame.myclient.sendMessageToServer(suspend);
							}
			}
		});
		btnSuspend.setBounds(169, 180, 89, 23);
		add(btnSuspend);
		
		JLabel lblEnterReason = new JLabel("Enter Reason:");
		lblEnterReason.setBounds(267, 88, 109, 14);
		add(lblEnterReason);
		
		JLabel lblInOrderTo_1 = new JLabel("In order to resume:");
		lblInOrderTo_1.setBounds(30, 226, 127, 14);
		add(lblInOrderTo_1);
		
		comboBox = new JComboBox();
		comboBox.setBounds(30, 251, 127, 20);
		add(comboBox);
		
		JButton btnResume = new JButton("Resume");
		btnResume.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem().equals("--Choose a Request--"))
				{
				(new Dialog("Please select a Request")).setVisible(true);
				}
					else
							{
						ArrayList<Object> suspend =new ArrayList<Object>();
						suspend.add("resume");
						suspend.add(comboBox.getSelectedItem());
						AcademicFrame.myclient.sendMessageToServer(suspend);
							}
			}
		});
		btnResume.setBounds(267, 250, 89, 23);
		add(btnResume);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
		});
		btnCancel.setBounds(169, 320, 89, 23);
		add(btnCancel);
		
		JButton btnReason = new JButton("Reason");
		btnReason.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem().equals("--Choose a Request--"))
				{
				(new Dialog("Please select a Request")).setVisible(true);
				}
					else
							{
						ArrayList<Object> suspend =new ArrayList<Object>();
						suspend.add("susInfo");
						suspend.add(comboBox.getSelectedItem());
						AcademicFrame.myclient.sendMessageToServer(suspend);
							}
				
			}
		});
		btnReason.setBounds(54, 282, 89, 23);
		add(btnReason);
	}

	
	
	
	
	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		
		if(answer.get(0).equals("allReq"))
		{
			if(answer.get(2).equals("0"))
			{
				eflag=1;
				this.rcomboBox.addItem("--Choose a Request--");
			}
			else{
				eflag=0;
			int numberOfreq = Integer.parseInt((answer.get(1)).toString()); 
			this.rcomboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfreq ; i++ )
			{
				this.rcomboBox.addItem((String)answer.get(i+2));  
			}
			ArrayList<Object> requests= new ArrayList<Object>();
			requests.add("getReq");
			requests.add("0");
			AcademicFrame.myclient.sendMessageToServer(requests); // get requests
			}
		}
		else
			if(answer.get(0).equals("ridlist"))
			{
				if(answer.get(2).equals("0"))
				{
					if(eflag==0){
						this.comboBox.addItem("--Choose a Request--");
					}
					else
					{
					(new Dialog("No Available Requests")).setVisible(true);
					frame.setSize(500, 500);
					frame.setContentPane(supervisorPanel);
					}
		
				}
				else
				{
				int numberOfreq = Integer.parseInt((answer.get(1)).toString()); 
				this.comboBox.addItem("--Choose a Request--");
				for (int i=0; i < numberOfreq ; i++ )
				{
					this.comboBox.addItem((String)answer.get(i+2));  
				}
				
				}
			}
		
			else
				if(answer.get(0).equals("suspend"))
				{
					(new Dialog("Suspended Successfully")).setVisible(true);
					frame.setContentPane(new SuspendResumePanel(frame, supervisorPanel));
					frame.setSize(460, 420);
				
					ArrayList<Object> req= new ArrayList<Object>();
					req.add("getAllReq");
					AcademicFrame.myclient.sendMessageToServer(req); // get requests
				
				}
				else
					if(answer.get(0).equals("resume"))
					{
						(new Dialog("Resumed Successfully")).setVisible(true);
						frame.setContentPane(new SuspendResumePanel(frame, supervisorPanel));
						frame.setSize(460, 420);
					
						ArrayList<Object> req= new ArrayList<Object>();
						req.add("getAllReq");
						AcademicFrame.myclient.sendMessageToServer(req); // get requests
					
					}
					else
						if(answer.get(0).equals("susInfo"))
						{
							(new DialogXL("Reason: "+answer.get(1))).setVisible(true);
						
						}
			
		
		}
		
	}

